<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2015 http://www.youdi365.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 心云间、凝听 <981248356@qq.com>
// +----------------------------------------------------------------------
namespace Admin\Controller;
use Think\Controller;
class PublicController extends Controller {
    protected $userModel;
    function _initialize()
    {
        $this->userModel = D('User');
    }
    /**
     * 后台登录
     */
    public function login(){

        if(session('user_login_auth')) redirect(U('Admin/Dashboard/index'));
        if (IS_POST) {
      			if(!check_verify(I('post.code'))){
      				//$this->error('验证码错误');
      			}
            $httppost = I('post.');//获取所有的post数据
            $username=$httppost['user_login'];
            $password=$httppost['password'];

            $user_info = $this->userModel->login($username, $password);
            if (!$user_info) {
                $this->error($this->userModel->getError());
            }

            // 设置登录状态
            if ($this->userModel->auto_login($user_info)) {
               $uid=$this->userModel->is_login();
            }

            // 跳转
            if (0 < $uid) {
                $this->success('登录成功！', U('Admin/Dashboard/index'));
            } else {
                $this->logout();
            }

        }else{
            $this->display();
        }
    }

    //退出登录
    public function logout(){
        $this->userModel->logout();
        session(null);
        redirect(U('/Admin/Public/login'));
        //$this->success('退出成功',U('/'));
    }

    //图片验证码
    public function verify_img(){
        verify(1);
    }
//清除缓存
public function delcache() { 
       header("Content-type: text/html; charset=utf-8");
        //清文件缓存
        $dirs = array('./Runtime/');
        @mkdir('Runtime',0777,true);
        //清理缓存
        foreach($dirs as $dir) {
         $this->rmdirr($dir);
        }
        $this->success('缓存已清除成功！');
 } 
 
/////////////下面是处理方法
    
 public function rmdirr($dirname) {
      if (!file_exists($dirname)) {
       return false;
      }
      if (is_file($dirname) || is_link($dirname)) {
       return unlink($dirname);
      }
      $dir = dir($dirname);
      if($dir){
       while (false !== $entry = $dir->read()) {
        if ($entry == '.' || $entry == '..') {
         continue;
        }
        //递归
        $this->rmdirr($dirname . DIRECTORY_SEPARATOR . $entry);
       }
      }
      $dir->close();
      return rmdir($dirname);
     }
}